package com.jhgsys.internal.dict.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.dict.entity.Dict;
import com.jhgsys.internal.dict.entity.DictItem;
import com.jhgsys.internal.dict.service.IDictItemService;
import com.jhgsys.internal.dict.service.IDictService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业务字典项 Controller
 *
 * @author chenweihua
 * @date 2020-05-22 15:28:32
 */
@Slf4j
@Validated
@Controller
public class DictItemController extends BaseController {

    @Autowired
    private IDictItemService dictItemService;

    @Autowired
    private IDictService dictService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "dictItem")
    @RequiresPermissions("dictItem:view")
    public String dictItemIndex(Model model){
        model.addAttribute("dictList", dictService.findDicts(new Dict()));
        return FebsUtil.view("dict/dictItem/dictItemList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "dictItem/add")
    @RequiresPermissions("dictItem:view")
    public String dictItemAdd(){
        return FebsUtil.view("dict/dictItem/dictItemEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "dictItem/update/{id}")
    @RequiresPermissions("dictItem:view")
    public String dictItemUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("dictItem", dictItemService.findDictItemById(id));
        return FebsUtil.view("dict/dictItem/dictItemEdit");
    }

    /**
     * 查询（所有）
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @GetMapping("dictItems")
    @ResponseBody
    @Cacheable(cacheNames = {"dict"})
    public FebsResponse getAllDictItems(String dictCode) {
        DictItem corn = new DictItem();
        corn.setDictCode(dictCode);
        return new FebsResponse().success().data(dictItemService.findDictItems(corn));
    }

    /**
     * 查询（分页）
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @GetMapping("dictItem/list")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public FebsResponse dictItemList(QueryRequest request, DictItem dictItem) {
        Map<String, Object> dataTable = getDataTable(this.dictItemService.findDictItems(request, dictItem));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 DictItem
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @Log("新增DictItem")
    @PostMapping("dictItem")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public FebsResponse addDictItem(@Valid DictItem dictItem) throws FebsException {
        try {
            dictItem.setCreatedBy(getCurrentUser().getUserName());
            this.dictItemService.createDictItem(dictItem);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增DictItem失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 DictItem
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @Log("逻辑删除DictItem")
    @GetMapping("dictItem/delete/{id}")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public FebsResponse deleteDictItem(@PathVariable("id") String dictItemIds) throws FebsException {
        try {
            DictItem dictItem = new DictItem();
            dictItem.setDeletedBy(getCurrentUser().getUserName());
            dictItem.setDeletedTime(new Date());
            dictItem.setDeletedFlg((byte)1);
            this.dictItemService.deleteDictItem(dictItem,dictItemIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DictItem失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 DictItem
    * @author chenweihua
    * @date 2020-05-22 15:28:32
    */
    @Log("物理删除DictItem")
    @GetMapping("dictItem/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public FebsResponse physicallyDeleteDictItem(@PathVariable("id") String dictItemIds) throws FebsException {
        try {
            this.dictItemService.physicallyDeleteDictItem(dictItemIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DictItem失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 DictItem
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @Log("修改DictItem")
    @PostMapping("dictItem/update")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public FebsResponse updateDictItem(DictItem dictItem) throws FebsException {
        try {
            dictItem.setUpdatedBy(getCurrentUser().getUserName());
            this.dictItemService.updateDictItem(dictItem);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改DictItem失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 DictItem的excel
     * @author chenweihua
     * @date 2020-05-22 15:28:32
     */
    @PostMapping("dictItem/excel")
    @ResponseBody
    @RequiresPermissions("dictItem:view")
    public void export(QueryRequest queryRequest, DictItem dictItem, HttpServletResponse response) throws FebsException {
        try {
            List<DictItem> dictItems = this.dictItemService.findDictItems(queryRequest, dictItem).getRecords();
            ExcelKit.$Export(DictItem.class, response).downXlsx(dictItems, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
